﻿package birdry.utils	{
	import flash.display.Graphics;
	import flash.display.Sprite;
	
	/**
	 * ...
	 * @author 
	 */
	public class  SpriteUtil	{
		public function SpriteUtil()	{
		}
		
		/**
		 * 선 그리기
		 * @param	nX0	:	x 시작점
		 * @param	nY0	:	y 시작점
		 * @param	nX1	:	x 끝나는점
		 * @param	nY1	:	y 끝나는점
		 */
		public static function drawLine(nX0:Number, nY0:Number, nX1:Number, nY1:Number):Sprite 		{
			var shape:Sprite = new Sprite();
			shape.graphics.moveTo(nX0, nY0);
			shape.graphics.lineTo(nX1, nY1);
			return shape;
		}
		
		/**
		 * 사각형 그리기
		 * @param	x
		 * @param	y
		 * @param	width
		 * @param	height
		 * @param	fillColor
		 * @param	alpha
		 * @return
		 */
		public static function drawRect(x:Number, y:Number, width:Number, height:Number, fillColor:uint = 0x000000, alpha:Number = 1 ):Sprite	 {
			var shape:Sprite = new Sprite();
			shape.graphics.beginFill(fillColor, alpha);
			shape.graphics.drawRect(x, y, width, height);
			shape.graphics.endFill();
			return shape;
		}
		
		/**
		 * 라운드 사각형 그리기
		 * @param	x
		 * @param	y
		 * @param	width
		 * @param	height
		 * @param	radius
		 * @param	fillColor
		 * @param	alpha
		 */
		public static function drawRoundRect(x:Number, y:Number, width:Number, height:Number, radius:Number, fillColor:uint = 0x000000, alpha:Number = 1):Sprite {
			var shape:Sprite = new Sprite();
			shape.graphics.beginFill(fillColor, alpha);
			shape.graphics.drawRoundRect(x, y, width, height, radius);
			shape.graphics.endFill();
			return shape;
		}
		
		
		/**
		 * 부분 라운드 사각형 그리기
		 * @param	width
		 * @param	height
		 * @param	nA
		 * @param	nB
		 * @param	nC
		 * @param	nD
		 * @param	fillColor
		 * @param	x
		 * @param	y
		 * @param	alpha
		 * @return
		 */
		public static function drawRoundRectComplex(x:Number, y:Number, width:Number, height:Number, nA:Number, nB:Number, nC:Number, nD:Number, fillColor:uint = 0x000000, alpha:Number = 1):Sprite {
			var shape:Sprite = new Sprite();
			shape.graphics.beginFill(fillColor, alpha);
			shape.graphics.drawRoundRectComplex(x, y, width, height, nA, nB, nC, nD);
			shape.graphics.endFill();
			return shape;
		}
		
		
		/**
		 * 원 그리기
		 * @param	radius
		 * @param	fillColor
		 * @param	x
		 * @param	y
		 * @param	alpha
		 */
		public static function drawCircle(x:Number, y:Number, radius:Number, fillColor:uint = 0x000000, alpha:Number = 1):Sprite {
			var shape:Sprite = new Sprite();
			shape.graphics.beginFill(fillColor, alpha);
			shape.graphics.drawCircle(x, y, radius);
			shape.graphics.endFill();
			return shape;
		}
		
		
		/**
		 * 타원 그리기
		 * @param	graphics
		 * @param	nX
		 * @param	nY
		 * @param	nRadiusX
		 * @param	nRadiusY
		 */
		/*
		public static function drawEllipse(graphics:Graphics, nX:Number, nY:Number, nRadiusX:Number, nRadiusY:Number):void {
			var nAngleDelta:Number = Math.PI / 4;
			var nAngle:Number = 0;
			var nCtrlDistX:Number = nRadiusX / Math.cos(nAngleDelta/2);
			var nCtrlDistY:Number = nRadiusY / Math.cos(nAngleDelta/2);
			var nCtrlX:Number;
			var nCtrlY:Number;
			var nAnchorX:Number;
			var nAnchorY:Number;
			graphics.moveTo(nX + nRadiusX, nY);
			for (var i:Number = 0; i < 8; i++) {
				nAngle += nAngleDelta;
				nCtrlX = nX + Math.cos(nAngle-(nAngleDelta/2))*(nCtrlDistX);
				nCtrlY = nY + Math.sin(nAngle-(nAngleDelta/2))*(nCtrlDistY);
				nAnchorX = nX + Math.cos(nAngle) * nRadiusX;
				nAnchorY = nY + Math.sin(nAngle) * nRadiusY;
				graphics.curveTo(nCtrlX, nCtrlY, nAnchorX, nAnchorY);
			}
		}
		*/
		
		/**
		 * 삼각형 그리기
		 * @param	nX				:	삼각형 중심점 x좌표
		 * @param	nY				:	삼각형 중심점 y좌표
		 * @param	nAB			:	점 a와 점 b를 잇는 선의길이
		 * @param	nAC			:	점 a와 점 c를 잇는 선의길이
		 * @param	nAngle		:	ab 와 ac 사이의 각도(degress)
		 * @param	nRotation	:	삼각형의 회전각
		 */
		/*
		public static function drawTriangle(graphics:Graphics, nX:Number, nY:Number, nAB:Number, nAC:Number, nAngle:Number, nRotation:Number = 0):void {
			nRotation = nRotation * Math.PI / 180;
			// Convert the angle between the sides from degrees to radians.
			nAngle = nAngle * Math.PI / 180;
			// Calculate the coordinates of points b and c.
			var nBx:Number = Math.cos(nAngle - nRotation) * nAB;
			var nBy:Number = Math.sin(nAngle - nRotation) * nAB;
			var nCx:Number = Math.cos(-nRotation) * nAC;
			var nCy:Number = Math.sin(-nRotation) * nAC;
			// Calculate the centroid's coordinates.
			var nCentroidX:Number = 0;
			var nCentroidY:Number = 0;
			// Move to point a, then draw line ac, then line cb, and finally ba (ab).
			graphics.moveTo(-nCentroidX + nX, -nCentroidY + nY);
			graphics.lineTo(nCx - nCentroidX + nX, nCy - nCentroidY + nY);
			graphics.lineTo(nBx - nCentroidX + nX, nBy - nCentroidY + nY);
			graphics.lineTo(-nCentroidX + nX, -nCentroidY + nY);
		}
		*/
		
		/**
		 * 정 다각형 그리기
		 * @param	nX				:	다각형 중심점의 x좌표
		 * @param	nY				:	다각형 중심점의 y좌표
		 * @param	nSides		:	다각형의 변의 수
		 * @param	nLength		:	각 변의 길이
		 * @param	nRotation	:	다각형의 회전각
		 */
		/*
		public static function drawRegularPolygon(graphics:Graphics, nX:Number, nY:Number, nSides:Number, nLength:Number, nRotation:Number = 0):void {
			nRotation = nRotation * Math.PI / 180;
			// The angle formed between the segments from the polygon's center as shown in 
			// Figure 4-5. Since the total angle in the center is 360 degrees (2p radians),
			// each segment's angle is 2p divided by the number of sides.
			var nAngle:Number = (2 * Math.PI) / nSides;
			// Calculate the length of the radius that circumscribes the polygon (which is
			// also the distance from the center to any of the vertices).
			var nRadius:Number = (nLength/2)/Math.sin(nAngle/2);
			// The starting point of the polygon is calculated using trigonometry where 
			// radius is the hypotenuse and nRotation is the angle.
			var nPx:Number = (Math.cos(nRotation) * nRadius) + nX;
			var nPy:Number = (Math.sin(nRotation) * nRadius) + nY;
			// Move to the starting point without yet drawing a line.
			graphics.moveTo(nPx, nPy);
			// Draw each side. Calculate the vertex coordinates using the same trigonometric
			// ratios used to calculate px and py earlier.
			for (var i:Number = 1; i <= nSides; i++) {
				nPx = (Math.cos((nAngle * i) + nRotation) * nRadius) + nX;
				nPy = (Math.sin((nAngle * i) + nRotation) * nRadius) + nY;
				graphics.lineTo(nPx, nPy);
			}
		}
		*/
		
		/**
		 * 호 그리기
		 * @param	nX						:	호의 중심 x좌표
		 * @param	nY						:	호의 중심 y좌표
		 * @param	nRadius				:	호의 반지름
		 * @param	nArc					:	호의 각도
		 * @param	nStartingAngle		:	호가 시작하는 각도 (default=0)
		 * @param	bRadialLines		:	호와 호의 중심을 이어주는 선을 그릴지여 여부
		 */
		/*
		public static function drawArc(graphics:Graphics, nX:Number, nY:Number, nRadius:Number, nArc:Number, nStartingAngle:Number = 0, bRadialLines:Boolean = false):void  {
			if(nArc > 360) {
				nArc = 360;
			}
			nArc = Math.PI/180 * nArc;
			var nAngleDelta:Number = nArc/8;
			var nCtrlDist:Number = nRadius/Math.cos(nAngleDelta/2);
			nStartingAngle *= Math.PI / 180;
			var nAngle:Number = nStartingAngle;
			var nCtrlX:Number;
			var nCtrlY:Number;
			var nAnchorX:Number;
			var nAnchorY:Number;
			var nStartingX:Number = nX + Math.cos(nStartingAngle) * nRadius;
			var nStartingY:Number = nY + Math.sin(nStartingAngle) * nRadius;
			if(bRadialLines) {
				Graphics(graphics).moveTo(nX, nY);
				Graphics(graphics).lineTo(nStartingX, nStartingY);
			}
			else {
				Graphics(graphics).moveTo(nStartingX, nStartingY);
			}
			for (var i:Number = 0; i < 8; i++) {
				nAngle += nAngleDelta;
				nCtrlX = nX + Math.cos(nAngle-(nAngleDelta/2))*(nCtrlDist);
				nCtrlY = nY + Math.sin(nAngle-(nAngleDelta/2))*(nCtrlDist);
				nAnchorX = nX + Math.cos(nAngle) * nRadius;
				nAnchorY = nY + Math.sin(nAngle) * nRadius;
				Graphics(graphics).curveTo(nCtrlX, nCtrlY, nAnchorX, nAnchorY);
			}
			if(bRadialLines) {
				Graphics(graphics).lineTo(nX, nY);
			}
		}
		*/
		
		/**
		 * 별 그리기
		 * @param	nX					:	별 중심점의 x좌표
		 * @param	nY					:	별 중심점의 y좌표
		 * @param	nPoints			:	별의 꼭지점 개수
		 * @param	nInnerRadius	:	별 안쪽의 반지름
		 * @param	nOuterRadius	:	별 바깥쪽의 반지름
		 * @param	nRotation		:	별의 회전각
		 */
		/*
		public static function drawStar(graphics:Graphics, nX:Number, nY:Number, nPoints:Number, nInnerRadius:Number, nOuterRadius:Number, nRotation:Number = 0):void {
			if(nPoints < 3) {
				return;
			}
			var nAngleDelta:Number = (Math.PI * 2) / nPoints;
			nRotation = Math.PI * (nRotation - 90) / 180;
			var nAngle:Number = nRotation;
			var nPenX:Number = nX + Math.cos(nAngle + nAngleDelta / 2) * nInnerRadius;
			var nPenY:Number = nY + Math.sin(nAngle + nAngleDelta / 2) * nInnerRadius;
			graphics.moveTo(nPenX, nPenY);
			nAngle += nAngleDelta;
			for(var i:Number = 0; i < nPoints; i++) {
				nPenX = nX + Math.cos(nAngle) * nOuterRadius;
				nPenY = nY + Math.sin(nAngle) * nOuterRadius;
				graphics.lineTo(nPenX, nPenY);
				nPenX = nX + Math.cos(nAngle + nAngleDelta / 2) * nInnerRadius;
				nPenY = nY + Math.sin(nAngle + nAngleDelta / 2) * nInnerRadius;
				graphics.lineTo(nPenX, nPenY);
				nAngle += nAngleDelta;
			}
		}
		*/
	}
}